<?php


use IDock\Insurance\MACN\ACICConstruct;
use IDock\Insurance\MACN\MACNConstruct;
use IDock\Insurance\MACN\Bean\Applicant;
use IDock\Insurance\MACN\Bean\Application;
use IDock\Insurance\MACN\Bean\CancelInfo;
use IDock\Insurance\MACN\Bean\CheckItem;
use IDock\Insurance\MACN\Bean\CoverageItem;
use IDock\Insurance\MACN\Bean\CustomerDetail;
use IDock\Insurance\MACN\Bean\Insured;
use IDock\Insurance\MACN\Bean\MFileItem;
use IDock\Insurance\MACN\Bean\Owner;
use IDock\Insurance\MACN\Bean\Taxation;
use IDock\Insurance\MACN\Bean\Vehicle;
use IDock\Insurance\MACN\Request\PremiumInquiryRequest;
use IDock\Insurance\MACN\Request\VehicleTypeQueryRequest;

require_once 'vendor/autoload.php';

$ACICConfig = [
    'gateway' => 'https://ply.e-acic.com/vchl-channel/services/accessService?wsdl',
    'EChannelId' => 'CH-F-HXYBX',
    'secretKey' => 'e435ffe2dcA2d61f5ea7e5e035b17cw8',
    'PCPayUrl' => 'https://plytst.e-acic.com/vchl-channel/upay/upay.htm',
    'WAPPayUrl' => 'https://plytst.e-acic.com/vchl-channel/upay/upayWap.htm',
    'logsDir' => './ACICLogs',
    'timeout' => '10',
    'ip' => '118.123.247.195',
    'proxy' => '118.123.247.195:45131',
];

//var_dump(json_encode($ACICConfig,true));exit;


$IDockACIC = new ACICConstruct($ACICConfig);
//
global $testResponse;

//$testResponse = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><soap:Fault><faultcode>soap:Server</faultcode><faultstring>HTTP verb was not GET or POST</faultstring></soap:Fault></soap:Body></soap:Envelope>';
/*$testResponse = '<?xml version="1.0" encoding="UTF-8"?><Packet>  <Head>    <RequestType>CH-F-HXYBX</RequestType>    <SerialNo>9a485db8d6f6537991f8bbe23e6c9c991bf2014a</SerialNo>    <ResponseCode>0</ResponseCode>    <ErrorCode>0000</ErrorCode>    <ErrorMessage>成功</ErrorMessage>  </Head><Body><Info><AppNo>599510803662023052499</AppNo><Status>0</Status></Info></Body></Packet>';*/

try {
    $licenseNo = '川A21M4B';
    $vin = 'LSJA24U67JS054422';
    $engineNo = '15328494836';
    $name = '詹强';
    $idCard = '511027197401084317';
    $address = '四川省简阳市禾丰镇响水村2组27号';
    $phone = '15328494836';
    $firstRegisterDate = '2018-06-02';


    //4.1.精准车型查询
    if (0) {
        $response = $IDockACIC->precisionVehicleQuery($licenseNo, $vin, '51', '');

        var_dump($response->isSuccess());
        var_dump($response->getError());
        var_dump($response->getEngineNo());
        var_dump($response->getEngineModel());
        var_dump($response->getModelList());
        exit;
    }

    //4.2.车型库查询
    //if (0) {
    //
    //    $response = $IDockACIC->vehicleTypeQuery('51', '长安', '', 'SC7155AABH5', '', '', '', '1', '1');
    //    var_dump($response->isSuccess());
    //    var_dump($response->getError());
    //    var_dump($response->getModelList());
    //    var_dump($response->getTotalCount());
    //    exit;
    //}

    //4.3.投保查询
    if (0) {
        $request = new PremiumInquiryRequest();
        //车辆信息
        $vehicle = new Vehicle();
        $vehicle->setLicensePlateNo($licenseNo);
        $vehicle->setMotorTypeCode(11);
        $vehicle->setMotorUsageTypeCode(210);
        $vehicle->setFirstRegisterDate($firstRegisterDate);
        $vehicle->setVIN($vin);//车架号
        $vehicle->setEngineNo($engineNo);//引擎号
        $vehicle->setModel('RWAAHD0028');//车型代码
        $vehicle->setReplacementValue('144800');
        $vehicle->setChangedVehlValue('0');
        $vehicle->setTransferDate('');
        $vehicle->setBrandType('');
        $vehicle->setDisplaceMent('1.49');
        $vehicle->setPower('124.0');
        $vehicle->setWholeWeight('1490');
        $vehicle->setFleetFlag(0);
        $vehicle->setFleetNo('');
        $vehicle->setIpmORLoc('LO');
        $vehicle->setEcdemicVehicleFlag('0');
        $vehicle->setCarName('荣威CSA6452NDAN运动型乘用车');//车名
        $vehicle->setRatedPassengerCapacity('5');
        $vehicle->setTonnage('');
        $vehicle->setHaulage('');
        $vehicle->setCSeat('5');//座位数
        $vehicle->setLoanVehicleFlag('0');
        $vehicle->setRegVchlType('K33');//轿车
        $vehicle->setAppointAreaCode('');
        $vehicle->setOutDangerYears('');
        $vehicle->setFuelType('0');
        $vehicle->setNewVchlFlag('1');
        $vehicle->setRegVchlUseType('');
        $vehicle->setLicensePlateType('');
        $vehicle->setRegVchlKey('');
        $vehicle->setRegVchlCheckCode('');
        $vehicle->setSaleCompany('');
        $vehicle->setSaleAreaCode('');
        $vehicle->setSale4SFlag('');
        $vehicle->setSHSumLimitType('');
        $vehicle->setPureRange('');
        $vehicle->setOtherEnergyTypeDesc('');
        $vehicle->setCAScore('');
        $vehicle->setMaxDesignSpeed('');
        $request->setVehicle($vehicle);

        //交强险
        $application1 = new Application();
        $application1->setProdNo('0330');
        $application1->setLastPolicyNo('');
        $application1->setBillDate(date('Y-m-d'));//投保日期
        $application1->setEffectiveDate('2023-05-07 00:00:00');
        $application1->setExpireDate('2024-05-06 23:59:59');
        $application1->setAreaCode('51');
        $application1->setAgentCode('');
        $application1->setAgentContract('');
        $application1->setExpectDiscount('');
        $application1->setSpecialContact('');
        $application1->setCountryCode('');
        $application1->setIsAgriculture('2');
        $application1->setIsForeign('0');
        $application1->setIsManufacture('0');
        $application1->setLitigationType('1');
        $application1->setLitigationOrg('');
        $application1->setOrgCode('51080503');//机构代码,必传
        $application1->setAgtSlsName('');
        $application1->setAgtSlsPhone('');
        $application1->setImmeEffFlag(0);
        $application1->setQueryNo('');
        $application1->setPreFreeDiscount('');
        $application1->setNewFuelVhlFlag('');
        $application1->setOrgCodeID('');
        $application1->setNewTransferDateFlag('');
        $request->setApplicationList($application1);

        //商业险
        $application2 = new Application();
        $application2->setProdNo('0333');
        $application2->setLastPolicyNo('');
        $application2->setBillDate('2023-05-05');//投保日期
        $application2->setEffectiveDate('2023-05-07 00:00:00');
        $application2->setExpireDate('2024-05-06 23:59:59');
        $application2->setAreaCode('51');
        $application2->setAgentCode('');
        $application2->setAgentContract('');
        $application2->setExpectDiscount('');
        $application2->setSpecialContact('');
        $application2->setCountryCode('');
        $application2->setIsAgriculture('2');
        $application2->setIsForeign('0');
        $application2->setIsManufacture('0');
        $application2->setLitigationType('1');
        $application2->setLitigationOrg('');
        $application2->setOrgCode('51080503');//机构代码,必传
        $application2->setAgtSlsName('');
        $application2->setAgtSlsPhone('');
        $application2->setImmeEffFlag(0);
        $application2->setQueryNo('');
        $application2->setPreFreeDiscount('');
        $application2->setNewFuelVhlFlag('');
        $application2->setOrgCodeID('');
        $application2->setNewTransferDateFlag('');
        $request->setApplicationList($application2);

        //商业险险种信息
        //三者
        $coverageItem1 = new CoverageItem();
        $coverageItem1->setCoverageCode('0101600');
        $coverageItem1->setSumLimit('1000000.00');
        $request->setCoverageList($coverageItem1);

        //车损
        $coverageItem2 = new CoverageItem();
        $coverageItem2->setCoverageCode('0101200');
        $coverageItem2->setSumLimit('109000');
        $request->setCoverageList($coverageItem2);

        //司机
        $coverageItem3 = new CoverageItem();
        $coverageItem3->setCoverageCode('0101701');
        $coverageItem3->setSumLimit('100000.00');
        $coverageItem3->setDaySeatNum(1);
        $request->setCoverageList($coverageItem3);

        //乘客
        $coverageItem4 = new CoverageItem();
        $coverageItem4->setCoverageCode('0101702');
        $coverageItem4->setDayFee('10000.00');
        $coverageItem4->setSumLimit('40000.00');
        $coverageItem4->setDaySeatNum(4);
        $request->setCoverageList($coverageItem4);


        //投保人信息
        $applicant = new Applicant();
        $applicant->setName($name);
        $applicant->setCertificateType('01');
        $applicant->setCertificateNo($idCard);
        $applicant->setContact($name);
        $applicant->setPhone($phone);
        $applicant->setAddress($address);
        $applicant->setOrganizationType('001');
        $applicant->setAge('');
        $applicant->setGender('');
        $applicant->setEmail('');
        $applicant->setVchlOwnership('2');
        $applicant->setBirthday('');
        $applicant->setType('');
        $applicant->setPostCode('');
        $applicant->setAddressDetail('');
        $applicant->setInsuredRelation('');
        //客户补充说明信息
        $customerDetail = new CustomerDetail();
        $customerDetail->setIDStartDate('2018-01-01');
        $customerDetail->setIDEndDate('2038-01-01');
        $customerDetail->setNation('CHN');
        $customerDetail->setIncome('28.4');
        $customerDetail->setWorkOrg('');
        $customerDetail->setCareerDetail(['CareerCode' => '2000005']);
        $customerDetail->setOrgDetail('');
        $customerDetail->setSHPayInfo('');
        $applicant->setCustomerDetail($customerDetail);
        $request->setApplicant($applicant);
        //被保人
        $insured = new Insured();
        $insured->setName($name);
        $insured->setCertificateType('01');
        $insured->setCertificateNo($idCard);
        $insured->setContact($name);
        $insured->setPhone($phone);
        $insured->setAddress($address);
        $insured->setGender('2');//女性
        $insured->setOrganizationType('001');
        $insured->setAge('');
        $insured->setEmail('');
        $insured->setVchlOwnership('2');
        $insured->setBirthday('1991-11-02');
        $insured->setType('');
        $insured->setPostCode('');
//    $insured->setAddressDetail('');
        $insured->setCustomerDetail($customerDetail);
        $request->setInsured($insured);
        //车主信息
        $owner = new Owner();
        $owner->setOwnerName($name);
        $owner->setCertificateType('01');
        $owner->setCertificateNo($idCard);
        $owner->setContact($name);
        $owner->setPhone($phone);
        $owner->setAddress($address);
        $owner->setOrganizationType('001');
        $owner->setAge('');
        $owner->setGender('');
        $owner->setEmail('');
        $owner->setBirthday('');
        $owner->setType('');
        $owner->setPostCode('');
        $owner->setAddressDetail('');
        $request->setOwner($owner);
        //车船税信息
        $taxation = new Taxation();
        $taxation->setTaxTermTypeCode('08');
        //$taxation->setTaxConditionCode('C');
        $taxation->setTaxConditionCode('T');
        $taxation->setTaxRegistryNumber('');
        $taxation->setTaxPayerName('杨林玉');
        $taxation->setTaxPayerIdentificationCode('510129197502253121');
        $taxation->setDeclareDate('2023-05-05');


        //本年纳税信息，如果存在已纳税或减免税才传此部分
        $currentTaxDue = [
            'TaxLocationCode' => '',
            'TaxStartDate' => '',
            'TaxEndDate' => '',
            'TaxUnitTypeCode' => '',
            'UnitRate' => '',
            'AnnualTaxAmount' => '',
            'Paid' => [ //完税信息
                //'TaxDepartmentCode' => '',
                //'TaxDepartment' => '',
                //'TaxDocumentNumber' => '',
                //'TaxDocumentDate' => '',
                //'TaxPaidAreaCode' => '',
            ],
            'Derate' => [ //减免税情况
                'DeductionDueCode' => 'E',
                'DeductionDueType' => 'P',
                'DeductionDueProportion' => '0.5',
                'Deduction' => '',
                'DeductionDocumentNumber' => '',
                'TaxDepartmentCode' => '',
                'TaxDepartment' => '四川税务局',
            ],
            'TaxDue' => '',
            'ExceedDate' => '',
            'ExceedDaysCount' => '',
            'OverDue' => '',
            'TotalAmount' => ''
        ];

        //$taxation->setCurrentTaxDue($currentTaxDue);

        $taxation->setDeclareStatusIA('');

        $request->setTaxation($taxation);

        $request->setAreaCode('51');

        //$response = $IDockACIC->insurancePriceQuery($request);
        //var_dump($response->isSuccess());
        //var_dump($response->getError());
        //var_dump($response->getPremium());
        //var_dump($response->getTotalSumlimit());
        //var_dump($response->getCI());
        //var_dump($response->getBI());
        //var_dump($response->getCHDRiskInfo());
        //var_dump($response->getBICoverages());
        //var_dump($response->getMFileList());
        //exit;
    }

    //4.5.投保预确认
    if (0) {
        //
        $applicant = new Applicant();
        $applicant->setName($name);
        $applicant->setCertificateType('01');
        $applicant->setCertificateNo($idCard);
        $applicant->setContact($name);
        $applicant->setPhone($phone);
        $applicant->setAddress($address);
        $applicant->setOrganizationType('001');
        //$applicant->setAge('');
        //$applicant->setGender('');
        //$applicant->setEmail('');
        $applicant->setVchlOwnership('2');
        //被保险人信息
//        $insured = new Insured();
//        $insured->setName('任倩');
//        $insured->setCertificateType('01');
//        $insured->setCertificateNo('510727197401050018');
//        $insured->setContact('张三');
//        $insured->setPhone('13912345678');
//        $insured->setAddress('四川省简阳市');
//        $insured->setOrganizationType('001');
//        $insured->setAge('30');
//        $insured->setGender('1');
//        $insured->setEmail('1@1.com');
//        $insured->setVchlOwnership('2');
        //车主信息
//        $owner = new Owner();
//        $owner->setOwnerName('张三');
//        $owner->setCertificateType('01');
//        $owner->setCertificateNo('500101198502260239');
//        $owner->setContact('张三');
//        $owner->setPhone('13912345678');
//        $owner->setAddress('四川省简阳市');
//        $owner->setOrganizationType('001');
//        $owner->setAge('30');
//        $owner->setGender('1');
//        $owner->setEmail('1@1.com');

        //车辆校验信息
        //$checkItem = new CheckItem();
        //$checkItem->setLicensePlateNo($licenseNo);
        //$checkItem->setMotorTypeCode('11');
        //$checkItem->setMotorUsageTypeCode(210);
        //$checkItem->setFirstRegisterDate($firstRegisterDate);
        //$checkItem->setVIN($vin);
        //$checkItem->setEngineNo($engineNo);
        //$checkItem->setModel('RWAAHD0028');
        //$checkItem->setReplacementValue('144800');
        //$checkItem->setChangedVehlValue('0');
        //$checkItem->setTransferDate('');
        //$checkItem->setBrandType('');
        ////$checkItem->setDisplaceMent('0.812');
        ////$checkItem->setPower('38.0');
        ////$checkItem->setWholeWeight('880');
        //$checkItem->setFleetFlag('0');//车队标志
        ////$checkItem->setFleetNo('');
        //$checkItem->setIpmORLoc('LO');
        //$checkItem->setEcdemicVehicleFlag('0');
        //$checkItem->setCarName('荣威CSA6452NDAN运动型乘用车');
        //$checkItem->setRatedPassengerCapacity('5');
        //$checkItem->setTonnage('');
        //$checkItem->setHaulage('');
        //$checkItem->setCSeat('5');
        //$checkItem->setLoanVehicleFlag('0');

        //保单信息
        //$application = new Application();
        //$application->setProdNo('0330');
        //$application->setBillDate('2023-04-28');
        //$application->setEffectiveDate('2023-05-01 00:00:00');
        //$application->setExpireDate('2024-05-01 00:00:00');
        //$application->setQueryNo('599510803602023000081');
        $application1->setQueryNo('599510803602023000091');
        $application2->setQueryNo('599510803662023000131');
        //$application->setAreaCode('51');
        $applicationList[] = $application1;
        $applicationList[] = $application2;

        //商业险险种信息
        //$coverageItem = new CoverageItem();
        //$coverageItem->setCoverageCode('0101200');
        //$coverageItem->setSumLimit('1000000.00');
        //$coverageItem->setFreeFlag('1');
        //$coverageItem->setFreeNum('');
        //$coverageItem->setDayFee('0');
        //$coverageItem->setDaySeatNum('0');
        $coverageList[] = $coverageItem1;
        $coverageList[] = $coverageItem2;
        $coverageList[] = $coverageItem3;
        $coverageList[] = $coverageItem4;

        $response = $IDockACIC->insuranceAdvanceConfirm($applicant, $applicant, $applicant, $applicationList, $vehicle, $coverageList);

        var_dump($response->isSuccess());
        var_dump($response->getError());
        var_dump($response->getInquiryNo());
        var_dump($response->getPremium());
        var_dump($response->getTotalSumlimit());
        var_dump($response->getCI());
        var_dump($response->getBI());
        var_dump($response->getUWFlag());
        var_dump($response->getUWMessage());
        var_dump($response->getBIInquiryNo());
        var_dump($response->getCIInquiryNo());
        exit;
    }

    if(0){
        //4.7影像资料上传
        $queryNo = 'A2015029310390121';//保险单号
        $fileItem = new MFileItem();
        $fileItem->setFileType('1001');//影像资料分类 1001:购车发票 1002:投保人身份证 1003:被保人身份证 1004:行驶证 1005:验车照 1006:验车报告 1007:完税凭证 1008:减免税凭证 1009:客户身份识别表 1010:上年保单证明资料 1019:其他资料 1101:合格证
        $fileItem->setFileURL('http://fanxinhome.com/600.png');//文件下载的url
        $fileItem->setFlag(0);
        $fileItem->setStream('');
        $fileList[] = $fileItem;//上传文件列表
        $response = $IDockACIC->uploadFile($queryNo,$fileList);

        var_dump($response->isSuccess());
        var_dump($response->getError());
        var_dump($response->getHeader());//todo 获取header详情
    }

    if(0){

        //4.8.投保单/保单状态查询
        $response = $IDockACIC->insuranceStateQuery('','599510803602023000081','');

        var_dump($response->isSuccess());
        var_dump($response->getError());
        var_dump($response->getCI());
        var_dump($response->getBI());
    }

    if(0){
        //4.10.续保查询
        $response = $IDockACIC->renewInsuranceQuery('VNNZF5251R6388HR8','ENGCLIEKHGGZXI84D','');

        var_dump($response->isSuccess());
        var_dump($response->getError());
        var_dump($response->getResultFlag());
        var_dump($response->getPolicy());
    }

    if(0){
        //4.13.投保注销
        $cancelInfo = new CancelInfo();
        $cancelInfo->setAppNo('599510803602023000081');
        $cancelInfo->setReason('客户注销');
        $response = $IDockACIC->cancelInsurance('51080503',[$cancelInfo]);

        var_dump($response->isSuccess());
        var_dump($response->getError());
        var_dump($response->getCancelResults());
    }

    //4.31.投保单撤回接口
    if(0){
        $response = $IDockACIC->returnToApply('599510803602023000081','0330');

        var_dump($response->isSuccess());
        var_dump($response->getError());
        var_dump($response->getInfo());
    }

    //4.32.影像上传结果查询
    if(0){
        $response = $IDockACIC->uploadResultQuery('599510803662023052284');

        var_dump($response->isSuccess());
        var_dump($response->getError());
        var_dump($response->getInfo());
        var_dump($response->getStatus());
        exit;
    }

    //4.6.3.渠道代收保单生成通知接口
    if(0){
        $response = $IDockACIC->channelInsuranceNotify('599500103602022000301','599500103662022000301','2323434234234','212121212');

        var_dump($response->isSuccess());
        var_dump($response->getError());
        var_dump($response->getPolicyList());
    }


} catch (Exception $e) {
    var_dump($e->getMessage());
}